# -*- coding: utf-8 -*-

from taskManager import walk,clean
from copy import copy
from time import time
from random import choice

class Auction():
	def __init__(self,robots,building,neverEnding=False,rand=False):
		self.robots=robots
		
		self.building=building
		rooms=self.building.floors.values()[0].rooms
		
		self.neverEnding=neverEnding
		
		self.rooms=copy(rooms)
		self.not_cleaned_rooms=copy(rooms)
		
		self.rand =rand
		
	def execute(self,current_time):
		try:
			self.init_time
		except:
			self.init_time = current_time
			self.executing = True
		if self.executing:
			if not self.distribute_orders(current_time) and not self.neverEnding:
				print "Tempo total = ",current_time-self.init_time
				print "Tempo de limpeza = ", sum([room.winner.score for room in self.rooms])
				print u"Tempo em deslocações = ",(current_time-self.init_time)*len(self.robots) - sum([room.winner.score for room in self.rooms])
				self.executing = False
	
	def distribute_orders(self,time):
		#peso = -dist_rob +dist_robs/num_robs
		none_to_be_done=False
		everyone_is_free=True
		for robot in self.robots:
			if robot.isFree():
				none_robot_is_free = False
				
				the_other_robots=copy(self.robots)
				the_other_robots.remove(robot)
				
				best_room=None
				best_room_score=None
				
				if self.neverEnding:
					self.not_cleaned_rooms=[room for room in self.rooms if room.needToBeCleaned(time)]
				
				if self.rand:
						try:
							best_room = choice(self.not_cleaned_rooms)
						except:
							best_room = None
				else:
					for room in self.not_cleaned_rooms:
						#~ print room
						dist_t_the_robot=self.building.path.shortestPathDist(room,robot.node0)
						
						dist_to_the_other_robots=0.0
						for one_robot in the_other_robots:
							dist_to_the_other_robots+=self.building.path.shortestPathDist(room,one_robot.node0)
							
						try:
							dist_to_the_other_robots/=len(the_other_robots)
						except:
							dist_to_the_other_robots=0.
						
						try:
							time_to_clean = room.winner.score+0.0
						except:
							time_to_clean = 0.0
						#~ 
						total_time_to_clean = sum([r.winner.score for r in self.not_cleaned_rooms])+0.0
						
						if time_to_clean>total_time_to_clean/(len(self.robots)) and len(self.robots)>1:
							time_to_clean_score=20
						else:
							time_to_clean_score=0.0
						#~ try:
							#~ time_to_clean_score = time_to_clean/total_time_to_clean*len(self.robots)*15
						#~ except ZeroDivisionError:
							#~ time_to_clean_score = 0.0
						#~ 
						
						score= - dist_t_the_robot + dist_to_the_other_robots*0.9 + time_to_clean_score# + time_to_clean/100
						#~ print room, score,"=",dist_t_the_robot*0.9,"+",dist_to_the_other_robots,"+",time_to_clean_score
						
						#~ score= - dist_t_the_robot + dist_to_the_other_robots + time_to_clean_score
						
						if score>best_room_score:
							best_room_score=score
							best_room=room
				
				#~ print best_room
				
				if(best_room):
					
					self.not_cleaned_rooms.remove(best_room)
					best_room.set_cleaned(time)
					clean(robot,best_room,self.building)
				else:
					none_to_be_done = True
			else:
				everyone_is_free=False
		
		return not (none_to_be_done and everyone_is_free)

